package com.example.apione.controller.admin;

import com.alibaba.fastjson2.JSONObject;
import com.example.apione.dao.ApiAccesslogDao;
import com.example.apione.dao.LoginDao;
import com.example.apione.dao.userLogDao;
import com.example.apione.entity.ApiAccesslog;
import com.example.apione.entity.userLog;
import com.example.apione.model.LoginApimodel;
import com.example.apione.utils.EncryptUtil;
import com.example.apione.utils.IPUtils;
import com.example.apione.utils.ResponseResult;
import lombok.Data;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;

/**
 * @author 尘落
 * @date 2023/2/24
 */
@Data
@RestController
public class Login {
    private final LoginDao loginDao;
    private final userLogDao userLogDao;
    @PostMapping("/api/login")
    public ResponseResult<String> login(@RequestBody HashMap<String,Object> body, HttpServletRequest request, HttpServletResponse response) {
        String username =(String) body.get("username");
        String password = (String) body.get("password");
        HashMap<String,String> adminData = LoginApimodel.login(loginDao);
        if (adminData.get("username").equals(username)&&
                adminData.get("password").equals(EncryptUtil.md5Code(password))){
            Cookie cookie=new Cookie("username",username);
            cookie.setMaxAge(60 * 60 * 24 * 30);
            cookie.setPath("/");
            response.addCookie(cookie);
            request.getSession().setAttribute("username",username);
            addLog(userLogDao,request);
            return ResponseResult.ok("登录成功！");
        }else {
            return ResponseResult.fail("账号或密码错误！");
        }

    }
    @GetMapping("/api/loginout")
    public ResponseResult<String> loginout( HttpServletRequest request, HttpServletResponse response) {
        Cookie cookie=new Cookie("username",null);
        cookie.setMaxAge(0);
        cookie.setPath("/");
        response.addCookie(cookie);
        request.getSession().removeAttribute("ussername");
        return ResponseResult.ok("退出成功！");

    }

    @PostMapping ("/admin/passwd")
    public ResponseResult<String> passwd( @RequestBody HashMap<String,Object> body) {
        String old_password =(String) body.get("old_password");
        String password = (String) body.get("password");
        HashMap<String,String> adminData = LoginApimodel.login(loginDao);
        if (adminData.get("password").equals(EncryptUtil.md5Code(old_password))){
              String retu =LoginApimodel.passwd(loginDao,password);
              return ResponseResult.ok("修改成功");
        }else {
            return ResponseResult.ok("密码不一致");
        }

    }


    public static   void addLog(userLogDao userLogDao, HttpServletRequest request){
        LocalDateTime createtime =LocalDateTime.now();
        IPutils iPutils=new IPutils();
        long createtime1=createtime.toEpochSecond(ZoneOffset.ofHours(8));
        String ip= IPUtils.getIpAddr(request);
        ResponseResult<JSONObject> getipmac = iPutils.getipmac(ip);
        String address=getipmac.getData().getString("city");
        if (address.isEmpty()){
            address=getipmac.getData().getString("province");
        }
        userLog userLog =new userLog();
        userLog.setIp(ip);
        userLog.setAddress(address);
        userLog.setCreatetime(String.valueOf(createtime1));
        userLogDao.insert(userLog);
    }
}
